home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: Derivation and calling virtual functions
- Message-ID: <1996Mar29.152758.1812@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 29 Mar 96 15:27:58 WET
- References: <graphix.828032689@spiff.cc.iastate.edu>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <graphix.828032689@spiff.cc.iastate.edu> graphix@iastate.edu
- (Kent A Vander Velden) writes:
-
- > Say we have the following:
- >
- > class Base {
- > public:
- > virtual void func() { // some stuff }
- > virtual void write() { func(); };
- > };
- >
- > class Derived : public Base {
- > public:
- > void func() { // some stuff }
- > write(); { Base::func(); }
- > };
- >
- > Derived inst;
- >
- > Now, when I call inst.write() it in turn calls Bass::write() which in
- > turn calls Base::func(). Is there a way to instead have it call
- > Derived::func() if the instance is of type Derived? I hoped the
- > virtual keyword would help in this case.
-
- You write Derived::write() will call Base::write(), but your code says
- Derived::write() calls Base::func(). If you change your code to have
- Derived::write() call Base::write(), then Base::write() will call the
- overriding Derived::func().
-
- Hope this helps,
-
- - Wil
-